package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/request"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SolarTermsApi struct {
}

// CreateSolarTerms 创建节气
// @Tags SolarTerms
// @Summary 创建节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customize.SolarTerms true "创建节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /solarTerms/createSolarTerms [post]
func (solarTermsApi *SolarTermsApi) CreateSolarTerms(c *gin.Context) {
	var solarTerms customize.SolarTerms
	err := c.ShouldBindJSON(&solarTerms)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := solarTermsService.CreateSolarTerms(&solarTerms); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteSolarTerms 删除节气
// @Tags SolarTerms
// @Summary 删除节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customize.SolarTerms true "删除节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /solarTerms/deleteSolarTerms [delete]
func (solarTermsApi *SolarTermsApi) DeleteSolarTerms(c *gin.Context) {
	var solarTerms customize.SolarTerms
	err := c.ShouldBindJSON(&solarTerms)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := solarTermsService.DeleteSolarTerms(solarTerms); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteSolarTermsByIds 批量删除节气
// @Tags SolarTerms
// @Summary 批量删除节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /solarTerms/deleteSolarTermsByIds [delete]
func (solarTermsApi *SolarTermsApi) DeleteSolarTermsByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := solarTermsService.DeleteSolarTermsByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateSolarTerms 更新节气
// @Tags SolarTerms
// @Summary 更新节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customize.SolarTerms true "更新节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /solarTerms/updateSolarTerms [put]
func (solarTermsApi *SolarTermsApi) UpdateSolarTerms(c *gin.Context) {
	var solarTerms customize.SolarTerms
	err := c.ShouldBindJSON(&solarTerms)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := solarTermsService.UpdateSolarTerms(solarTerms); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindSolarTerms 用id查询节气
// @Tags SolarTerms
// @Summary 用id查询节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customize.SolarTerms true "用id查询节气"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /solarTerms/findSolarTerms [get]
func (solarTermsApi *SolarTermsApi) FindSolarTerms(c *gin.Context) {
	var solarTerms customize.SolarTerms
	err := c.ShouldBindQuery(&solarTerms)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if solarTermsInfo, err := solarTermsService.GetSolarTerms(solarTerms.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(solarTermsInfo, c)
	}
}

// GetSolarTermsList 分页获取节气列表
// @Tags SolarTerms
// @Summary 分页获取节气列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.SolarTermsSearch true "分页获取节气列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /solarTerms/getSolarTermsList [get]
func (solarTermsApi *SolarTermsApi) GetSolarTermsList(c *gin.Context) {
	var pageInfo customizeReq.SolarTermsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := solarTermsService.GetSolarTermsInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (solarTermsApi *SolarTermsApi) SetSolarTermsArticle(c *gin.Context) {
	var req customizeReq.SolarTermsArticleReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := solarTermsService.SetSolarTermsArticle(req); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

func (solarTermsApi *SolarTermsApi) GetSolarTermsArticle(c *gin.Context) {
	solarTermsID := c.Query("solarTermsID")
	if res, err := solarTermsService.GetSolarTermsArticle(solarTermsID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(res, c)
	}
}
